package com.mo.lib.view.sideletter

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.dylanc.longan.sp
import com.mo.lib.constans.CityBean

class SideLetterBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    private var choose = -1
    private val paint = Paint()
    private var showBg = false
    private var onLetterChangedListener: OnLetterChangedListener? = null
    private var tipsView: TextView? = null
    private var texColor: Int = Color.parseColor("#5c5c5c")
    private var letterData = mutableListOf<String>("定位", "热门", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")


    /** 设置悬浮提示的textview*/
    fun setOverlay(overlay: TextView?) {
        this.tipsView = overlay
    }

    fun setColor(color: String) {
        texColor = Color.parseColor(color)
        invalidate()
    }

    fun setList(list: MutableList<String>) {
        letterData = list
        invalidate()
    }

    fun setCityList(list: MutableList<CityBean>) {
        val mutableListOf = mutableListOf<String>()
        list.forEach {
            if (it.name == "热门") {
                mutableListOf.add("热门")
            } else if (it.name == "定位") {
                mutableListOf.add("定位")
            } else {
                mutableListOf.add(it.topc)
            }
        }
        letterData = mutableListOf.distinct() as MutableList<String>
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (showBg) {
            canvas.drawColor(Color.TRANSPARENT)
        }
        val height = height
        val width = width
        val singleHeight = height / letterData.size
        for (i in letterData.indices) {
            paint.textSize = 13.sp
            paint.color = texColor
            paint.isAntiAlias = true //            if (i == choose) {
            //                paint.color = Color.parseColor("#3a8eff") //                paint.setFakeBoldText(true);  //加粗
            //            }
            val xPos = width / 2 - paint.measureText(letterData[i]) / 2
            val yPos = (singleHeight * i + singleHeight).toFloat()
            canvas.drawText(letterData[i], xPos, yPos, paint)
            paint.reset()
        }
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        val action = event.action
        val y = event.y
        val oldChoose = choose
        val listener = onLetterChangedListener
        val c = (y / height * letterData.size).toInt()
        when (action) {
            MotionEvent.ACTION_DOWN -> {
                showBg = true
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < letterData.size) {
                        listener.onLetterChanged(letterData[c])
                        choose = c
                        invalidate()
                        if (tipsView != null) {
                            tipsView!!.visibility = VISIBLE
                            tipsView!!.text = letterData[c]
                        }
                    }
                }
            }
            MotionEvent.ACTION_MOVE -> if (oldChoose != c && listener != null) {
                if (c >= 0 && c < letterData.size) {
                    listener.onLetterChanged(letterData[c])
                    choose = c
                    invalidate()
                    if (tipsView != null) {
                        tipsView!!.visibility = VISIBLE
                        tipsView!!.text = letterData[c]
                    }
                }
            }
            MotionEvent.ACTION_UP -> {
                showBg = false
                choose = -1
                invalidate()
                if (tipsView != null) {
                    tipsView!!.visibility = GONE
                }
            }
        }
        return true
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return super.onTouchEvent(event)
    }

    fun setOnLetterChangedListener(onLetterChangedListener: OnLetterChangedListener?) {
        this.onLetterChangedListener = onLetterChangedListener
    }

    interface OnLetterChangedListener {
        fun onLetterChanged(letter: String?)
    }
}